#include "LoginDialog.h"

LoginDialog::LoginDialog(QWidget* parent)
    : QDialog(parent),
    m_networkManager(new NetworkManager(this))
{
    initUi();
    connectsignalToSolt();
    setStyle();
    
}

void LoginDialog::initUi()
{
    setWindowTitle("登录聊天室");

    usernameLabel = new QLabel("用户名:");
    passwordLabel = new QLabel("密码:");
    serverLabel = new QLabel("服务器:");
    portLabel = new QLabel("端口:");

    m_usernameEdit = new QLineEdit();
    m_passwordEdit = new QLineEdit();
    m_passwordEdit->setEchoMode(QLineEdit::Password);
    m_serverHostEdit = new QLineEdit("192.168.171.128");
    m_serverPortEdit = new QLineEdit("8081");

    m_connectServerBtn= new QPushButton("连接服务器");
    m_loginBtn = new QPushButton("登录");
    m_registerBtn=new QPushButton("注册");
    m_cancelBtn = new QPushButton("取消");



    auto* formLayout = new QVBoxLayout();
    formLayout->addWidget(usernameLabel);
    formLayout->addWidget(m_usernameEdit);
    formLayout->addWidget(passwordLabel);
    formLayout->addWidget(m_passwordEdit);
    formLayout->addWidget(serverLabel);
    formLayout->addWidget(m_connectServerBtn);
    formLayout->addWidget(m_serverHostEdit);
    formLayout->addWidget(portLabel);
    formLayout->addWidget(m_serverPortEdit);

    auto* buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(m_registerBtn);
    buttonLayout->addWidget(m_loginBtn);
    buttonLayout->addWidget(m_cancelBtn);

    auto* mainLayout = new QVBoxLayout(this);
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(buttonLayout);
}

void LoginDialog::connectsignalToSolt()
{
    connect(m_loginBtn, &QPushButton::clicked, this, &LoginDialog::onLoginClicked);
    connect(m_cancelBtn, &QPushButton::clicked, this, &LoginDialog::onCancelClicked);
    connect(m_registerBtn, &QPushButton::clicked, this, &LoginDialog::onRegisterClicked);
    connect(m_connectServerBtn, &QPushButton::clicked, this, &LoginDialog::onConnectServerClicked);
    connect(this,&LoginDialog::loginSignal, m_networkManager, &NetworkManager::login);
    connect(this, &LoginDialog::connectServerSignal, m_networkManager, &NetworkManager::connectToServer);
    
}

QString LoginDialog::getUsername() const
{
    return m_usernameEdit->text().trimmed();
}

QString LoginDialog::getPassword() const
{
    return m_passwordEdit->text();
}

QString LoginDialog::getServerHost() const
{
    return m_serverHostEdit->text().trimmed();
}

quint16 LoginDialog::getServerPort() const
{
    return m_serverPortEdit->text().toUShort();
}

void LoginDialog::onLoginClicked()
{
    QString username = m_usernameEdit->text();
    QString password = m_passwordEdit->text();

    if (username.isEmpty() || password.isEmpty()) {
        return;
    }

    emit loginSignal(username, password);
}

void LoginDialog::onCancelClicked()
{
    reject();
}

void LoginDialog::setStyle()
{
    this->setObjectName("loginDialog");
    usernameLabel->setObjectName("usernameLabel");
    passwordLabel->setObjectName("passwordLabel");
    serverLabel->setObjectName("serverLabel");
    portLabel->setObjectName("portLabel");

    m_usernameEdit->setObjectName("usernameEdit");
    m_passwordEdit->setObjectName("passwordEdit");
    m_serverHostEdit->setObjectName("serverHostEdit");
    m_serverPortEdit->setObjectName("serverPortEdit");

    m_loginBtn->setObjectName("loginBtn");
    m_cancelBtn->setObjectName("cancelBtn");


}

void LoginDialog::onConnectServerClicked()
{
    emit connectServerSignal(getServerHost(), getServerPort());
}

void LoginDialog::onRegisterClicked()
{
    QString username = getUsername();
    QString password = getPassword();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "用户名和密码不能为空");
        return;
    }

    m_networkManager->registerAccount(username, password);


}
